package com.controller;


import com.common.Result;
import com.common.ResultEnum;
import com.common.log.OperationLog;
import com.common.log.OperationType;
import com.entity.Clock;
import com.service.ClockService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author clay
 * @since 2022-03-27
 */
@RestController
@RequestMapping("/clock")
public class ClockController {
    @Resource
    private ClockService clockService;

    @ApiOperation("添加打卡信息")
    @OperationLog(operationType = OperationType.INSERT, describe = "添加打卡信息", entity = Clock.class, primaryKey = "id")
    @PostMapping(value = "/add")
    public Result addDepartment(@RequestBody @Valid Clock clock) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //判断是不是签到
        if ("签到".equals(clock.getClockType())) {

            //获取当前时间
            String format = simpleDateFormat.format(System.currentTimeMillis());
            Date nowTime = simpleDateFormat.parse(format);

            //获取今天的9:30
            Date startTime = simpleDateFormat.parse(format.substring(0, 10) + " 09:30:00");


            //比较当前签到时间是不是在9:30之前
            if (nowTime.before(startTime)) {
                //赋值
                clock.setCreateTime(format);
                clock.setLateType("0");
                //添加到数据库中
                clockService.save(clock);
                return Result.success("200", "签到成功，新的一天请好好工作呀~");
            } else {
                //赋值
                clock.setCreateTime(format);
                clock.setLateType("1");
                //添加到数据库中
                clockService.save(clock);
                return Result.success("400", "您今天迟到啦，请及时上报迟到原因o(π π)o");
            }
        }

        if ("签退".equals(clock.getClockType())) {
            //获取当前时间
            String format = simpleDateFormat.format(System.currentTimeMillis());
            Date nowTime = simpleDateFormat.parse(format);
            //获取今天的18:30
            Date endTime = simpleDateFormat.parse(format.substring(0, 10) + " 18:30:00");

            //比较当前签到时间是不是在18:30之后
            if (nowTime.after(endTime)) {
                //赋值
                clock.setCreateTime(format);
                clock.setLeaveEarlyType("0");
                //添加到数据库中
                clockService.save(clock);
                return Result.success("200", "签退成功，新的一天请好好工作呀~");
            } else {
                //赋值
                clock.setCreateTime(format);
                clock.setLeaveEarlyType("1");
                //添加到数据库中
                clockService.save(clock);
                return Result.success("400", "您今天早退啦，请于明天上午10:30前上报早退原因o(π π)o");
            }
        } else {
            return Result.fail(ResultEnum.ERROR.getCode(), "签退失败，请重试");
        }


    }

    @ApiOperation("删除打卡信息")
    @OperationLog(operationType = OperationType.DELETE, describe = "删除打卡", entity = Clock.class, primaryKey = "id")
    @GetMapping(value = "/delete/{id}")
    public Result deleteDepartment(@PathVariable("id") String id) {
        clockService.removeById(id);
        return Result.success();
    }

    @ApiOperation("通过id获取打卡信息")
    @GetMapping(value = "/info/{id}")
    public Result info(@PathVariable("id") String id) {
        return Result.success(clockService.getById(id));
    }

    @ApiOperation("查询打卡信息")
    @PostMapping(value = "/select")
    public Result selectDepartment(@RequestBody Clock clock) {
        return Result.success(clockService.select(clock));
    }
}
